package cn.tsmchina.one.controller.m;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tsmchina.one.domain.activity.Activity;
import cn.tsmchina.one.domain.product.Product;
import cn.tsmchina.one.service.activity.ActivityService;
import cn.tsmchina.one.service.product.ProductService;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.JsonUtil;

@Controller
public class DetailController
{
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ActivityService activityService;
	
	@RequestMapping("/detail/{id}.html")
	public String detail(@PathVariable("id") long id,Model model)
	{
		Product product = productService.getProduct(id);
		if(product==null)
			return "m/404";
		Map<String, Object> data = new HashMap<>();
		data.put("id", product.getId());
		data.put("name", product.getProductName());
		data.put("price",product.getPrice());
		data.put("number", 1);
		data.put("images", product.getImages());
		//data.put("", value)
		model.addAttribute("p", product);
		model.addAttribute("product", JsonUtil.parseJson(data));
		model.addAttribute("title", product.getProductName());
		return "m/detail";
	}
	
	
	@RequestMapping("/detailActivity/{id}.html")
	public String detailActivity(@PathVariable("id") int id,Model model)
	{
		Activity activity = activityService.getActivity(id);
		model.addAttribute("activity", activity);
		return "m/detailActivity";
	}
	
	@RequestMapping("/joinActivity")
	@ResponseBody
	public Object detailActivity(HttpServletRequest request)
	{
		String userId = request.getParameter("userId");
		String activityId = request.getParameter("id");
		
		Map<String, Object> data = new HashMap<>();
		
		if(!StringUtils.isEmpty(activityId) && !StringUtils.isEmpty(userId)){
			Integer id = Integer.valueOf(activityId);
			
			Activity activity = activityService.getActivity(id);
			String joinUserIds = activity.getJoinUserId();
			
			
			//尚未有人参与
			if(StringUtils.isEmpty(joinUserIds)){
				activity.setJoinUserId(userId);
				activityService.updateActivity(activity);
				
				data.put("ok", C.Msg.OK);
				data.put("msg", "参加成功!");
				
				return data;
			}else{
				//未参与
				if(joinUserIds.indexOf(userId) <0 ){
					String joinId = activity.getJoinUserId();
					activity.setJoinUserId(joinId+","+userId);
					activityService.updateActivity(activity);
					
					data.put("ok", C.Msg.OK);
					data.put("msg", "参加成功!");
					
					return data;
				}else{
					data.put("ok", C.Msg.OK);
					data.put("msg", "你已经参加了!");
					
					return data;
				}
			}
		}
		
		data.put("ok", C.Msg.ERROR);
		data.put("msg", "参加失败!");
		return data;
	}
	
	
}
